<template>
  <div class="app-container">

    <!-- 搜索工作栏 -->
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="100px">
      <el-form-item label="项目名称" prop="name">
        <el-select v-model="queryParams.projectId" multiple clearable collapse-tags size="small" placeholder="请输入项目名称" @change="handleQuery">
          <el-option v-for="project in projectDatas"
                       :key="project.id" :label="project.projectName" :value="parseInt(project.id)" />
        </el-select>
      </el-form-item>
      <el-form-item label="描述" prop="doTaskDesc">
        <el-input v-model="queryParams.doTaskDesc" placeholder="请输入描述" clearable size="small" />
      </el-form-item>
      <el-form-item label="开始日期">
        <el-date-picker v-model="dateRangeEndTime" size="small" style="width: 240px" value-format="yyyy-MM-dd"
                        type="daterange" range-separator="-" start-placeholder="开始日期" end-placeholder="结束日期" />
      </el-form-item>
      <el-form-item label="一级分类" prop="doTaskUid">
        <el-select v-model="queryParams.maintainId" placeholder="请选择一级分类" clearable  size="small" @change="changeFirst">
          <el-option v-for="dict in firstLevelDatas"
                       :key="dict.value" :label="dict.label" :value="parseInt(dict.value)" />
        </el-select>
      </el-form-item>
      <el-form-item label="二级分类" prop="projectId">
        <el-select v-model="queryParams.subMaintainId" placeholder="请选择二级分类" clearable  size="small" @keyup.enter.native="handleQuery">
          <el-option v-for="dict in secondLevelDatas"
                       :key="dict.value" :label="dict.label" :value="parseInt(dict.value)" />
        </el-select>
      </el-form-item>
      <el-form-item label="执行者" prop="currentLocation">
        <user-select ref="UserSelect" :pModel.sync="queryParams.doTaskUid" placeholder="请选择执行者" :selectChange="handleQuery" @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="状态" prop="isFinish">
        <el-select v-model="queryParams.isFinish" placeholder="请选择状态"  @keyup.enter.native="handleQuery" clearable size="small" @change="handleQuery">
          <el-option v-for="dict in statusDictDatas"
                       :key="dict.value" :label="dict.label" :value="dict.value"/>
        </el-select>
        <el-form-item label="结束日期">
        <el-date-picker v-model="dateRangeDoTime" size="small" style="width: 240px" value-format="yyyy-MM-dd"
                        type="daterange" range-separator="-" start-placeholder="结束日期" end-placeholder="结束日期" />
      </el-form-item>
      </el-form-item>
      <el-form-item label="项目经理" prop="projectLeaderUid">
        <user-select ref="UserSelect" :pModel.sync="queryParams.projectLeaderUid" placeholder="请选择项目经理" roleId="11" :selectChange="handleQuery" @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="项目主管" prop="directorUid">
        <user-select ref="UserSelect" :pModel.sync="queryParams.directorUid" placeholder="请选择项目主管" roleId="12" :selectChange="handleQuery" @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="区域经理" prop="areaManagerUid">
        <user-select ref="UserSelect" :pModel.sync="queryParams.areaManagerUid" placeholder="请选择区域经理" roleId="10" :selectChange="handleQuery" @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <!-- 操作工具栏 -->
    <el-row :gutter="10" class="mb8">
      <!-- <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
                   v-hasPermi="['fi:coreTask:create']">新增</el-button>
      </el-col> -->
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
                   v-hasPermi="['fi:coreTask:export']">导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>
    <duo-image-viewer 
      :src="imageViewer.src" 
      :srcList="imageViewer.srcList" 
      @close="handleClose" 
      :showViewer="imageViewer.showViewer"/>
    <!-- 列表 -->
    <el-table v-loading="loading" :data="list">
      <el-table-column label="养护名称" align="center" prop="maintainId" >
        <template slot-scope="scope">
          <router-link :to="'/fi/coreTask/detail/' + scope.row.id" class="link-type">
          <span>{{ getDictDataLabel(DICT_TYPE.FI_PROJECT_FEEDBACK_TYPE, scope.row.maintainId) }}</span>
          </router-link>
        </template>
      </el-table-column>
      <el-table-column label="项目名称" align="center" prop="projectName" />
      <el-table-column label="任务状态" align="center" prop="isFinish" >
        <template slot-scope="scope">
          <span>{{ getDictDataLabel(DICT_TYPE.FI_CORE_TASK_STATUS, scope.row.isFinish) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="执行者" align="center" prop="doTaskName" />
      <el-table-column label="完成描述" align="center" prop="doTaskDesc" />
      <el-table-column label="更新时间" align="center" prop="finishDate" width="120">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.finishDate) }}</span>
        </template>
      </el-table-column>-
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text"  @click="handleDetail(scope.row)"
                     v-hasPermi="['fi:coreTask:create']">详情</el-button>
          <el-button size="mini" type="text"  @click="handleDelete(scope.row)"
                     v-hasPermi="['fi:coreTask:delete']">删除</el-button>
          <el-button size="mini" type="text"  @click="handleOpen(scope.row)">
                     预览图片</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页组件 -->
    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNo" :limit.sync="queryParams.pageSize"
                @pagination="getList"/>
  </div>
</template>

<script>
import { create, update, delCoreTask, get, getPage, exportExcel } from "@/api/fi/coreTask/";
import { listTree } from "@/api/system/dict/data";
import UserSelect from '@/components/UserSelect';
import { getDictDataLabel, getDictDatas, DICT_TYPE } from '@/utils/dict';
import {getProjectList} from '@/api/fi/project';
export default {
  name: "CoreTask",
  components: {
    UserSelect
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 项目反馈列表
      list: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      dateRangeCreateTime: [],
      dateRangeFinishDate: [],
      dateRangeEndTime: [],
      dateRangeDoTime: [],
      // 查询参数
      queryParams: {
        pageNo: 1,
        pageSize: 10,
        name: null,
        maintainId: null,
        doTaskUid: null,
        projectId: null,
        isFinish: null,
        doTaskDesc: null,
        projectLeaderUid: null,
        areaManagerUid : null,
        directorUid : null
      },
      imageViewer : {
        src : null,//对象
        srcList : null,//数组
        showViewer : false
      },
      firstLevelDatas : [],
      secondLevelDatas : [],
      // 表单参数
      form: {},
      projectDatas : [],
     /*  // 表单校验
      rules: {
        name: [{ required: true, message: "养护名称不能为空", trigger: "blur" }],
        maintainId: [{ required: true, message: "养护护类型不能为空", trigger: "blur" }],
        doTaskUid: [{ required: true, message: "实施者不能为空", trigger: "blur" }],
        projectId: [{ required: true, message: "项目id不能为空", trigger: "blur" }],
        isFinish: [{ required: true, message: "状态不能为空", trigger: "blur" }],
        currentLocation: [{ required: true, message: "当前地址不能为空", trigger: "blur" }],
        latLongItude: [{ required: true, message: "坐标不能为空", trigger: "blur" }],
        finishDate: [{ required: true, message: "完成时间不能为空", trigger: "blur" }],
        taskType: [{ required: true, message: "任务类型不能为空", trigger: "change" }],
      }, */
      statusDictDatas: getDictDatas(DICT_TYPE.FI_CORE_TASK_STATUS)
    };
  },
  created() {
    this.getList();
    this.getMaintainOptions();
    this.getProjectList();
  },
  methods: {
    /** 查询列表 */
    getList() {
      this.loading = true;
      // 处理查询参数
      let params = {...this.queryParams};
      this.addBeginAndEndTime(params, this.dateRangeCreateTime, 'createTime');
      this.addBeginAndEndTime(params, this.dateRangeFinishDate, 'finishDate');
      this.addBeginAndEndTime(params, this.dateRangeEndTime, 'endTime');
      this.addBeginAndEndTime(params, this.dateRangeDoTime, 'doTime');
      // 执行查询
      getPage(params).then(response => {
        this.list = response.data.list;
        this.total = response.data.total;
        this.loading = false;
      });
    },
    //获取项目下拉框
    getProjectList(){
      var params = {};
      // 执行查询
      getProjectList(params).then(response => {
        this.projectDatas = response.data;
      });
    },
    //获取一级分类
    getMaintainOptions(){
      const params = {
        dictType : DICT_TYPE.FI_PROJECT_FEEDBACK_TYPE
      }
      listTree(params).then(response => {
        this.firstLevelDatas = response.data;
      });
    },
    changeFirst(val){
      let node = this.firstLevelDatas.find(item => item.id===val);
      this.secondLevelDatas = node.children;
      this.handleQuery();
    },
    /** 取消按钮 */
    cancel() {
      this.open = false;
      this.reset();
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNo = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRangeCreateTime = [];
      this.dateRangeFinishDate = [];
      this.dateRangeEndTime = [];
      this.dateRangeDoTime = [];
      this.queryParams.maintainId = null;
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加项目反馈";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id;
      get(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改项目反馈";
      });
    },
    handleDetail(row){
      const taskId = row.id;
      this.$router.push({path: "/fi/coreTask/detail/"+taskId});
    },
    handleOpen(row) {
      const imgs = row.doImgList.split(",");
      this.imageViewer.src = imgs[0];
      this.imageViewer.srcList = this.imgs;
      this.imageViewer.showViewer = !this.showViewer
    },
    handleClose() {
      this.imageViewer.src = null;
      this.imageViewer.srcList = null;
      this.imageViewer.showViewer = false
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (!valid) {
          return;
        }
        // 修改的提交
        if (this.form.id != null) {
          update(this.form).then(response => {
            this.msgSuccess("修改成功");
            this.open = false;
            this.getList();
          });
          return;
        }
        // 添加的提交
        create(this.form).then(response => {
          this.msgSuccess("新增成功");
          this.open = false;
          this.getList();
        });
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const id = row.id;
      this.$confirm('是否确认删除项目反馈编号为"' + id + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delCoreTask(id);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
    },
    /** 导出按钮操作 */
    handleExport() {
      // 处理查询参数
      let params = {...this.queryParams};
      params.pageNo = undefined;
      params.pageSize = undefined;
      this.addBeginAndEndTime(params, this.dateRangeCreateTime, 'createTime');
      this.addBeginAndEndTime(params, this.dateRangeFinishDate, 'finishDate');
      this.addBeginAndEndTime(params, this.dateRangeEndTime, 'endTime');
      this.addBeginAndEndTime(params, this.dateRangeDoTime, 'doTime');
      // 执行导出
      this.$confirm('是否确认导出所有项目反馈数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportExcel(params);
        }).then(response => {
          this.downloadExcel(response, '项目反馈.xls');
        })
    }
  }
};
</script>
